#include "ultrasonic.h"

extern float Hcsr04GetLength;


void set_nonblocking(int sockfd) {
    int flags = fcntl(sockfd, F_GETFL, 0);
    if (flags < 0) {
        perror("fcntl get");
        return;
    }
    if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
        perror("fcntl set");
    }
}

void send_image(int connfd){
	//设置拍照
	
	//set_nonblocking(connfd);
	
	FILE *fp;
    	char buffer[BUFFER_SIZE] = {0};
    	const char *device_path = "/dev/video0"; // 设备路径
    	const char *output_file_path = "photo.jpg"; // 输出文件路径
    	int width = 640; // 图像宽度
    	int height = 480; // 图像高度
    	int pixel_format = V4L2_PIX_FMT_MJPEG; // 图像格式
    	int serial_fd =serial_open("dev/ttySAC2");
    	if (serial_fd < 0) {
        	perror("open serial");
        	return;
    	}
    	serial_config(serial_fd);
	//拍照上传
	//cam.c
	take_picture(device_path, output_file_path, width, height, pixel_format);
	printf("take photo success: %s\n", output_file_path);
	fp = fopen("photo.jpg", "rb");
	if (fp == NULL) {
		perror("fopen");
			return;
	}
	// 发送数据
	size_t n;
	while ((n = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
		send(connfd, buffer, n, 0);
	}		
	printf("File sent successfully\n");	
    	//serial_close(sfd);


}

#if 0
void send_image(int connfd){
	//设置拍照
	
	//set_nonblocking(connfd);
	
	FILE *fp;
    	char buffer[BUFFER_SIZE] = {0};
    	const char *device_path = "/dev/video0"; // 设备路径
    	const char *output_file_path = "photo.jpg"; // 输出文件路径
    	int width = 640; // 图像宽度
    	int height = 480; // 图像高度
    	int pixel_format = V4L2_PIX_FMT_MJPEG; // 图像格式
    	int serial_fd =serial_open("dev/ttySAC2");
    	if (serial_fd < 0) {
        	perror("open serial");
        	return;
    	}
    	serial_config(serial_fd);

	#if 0
	//读距离
	char buf[1024];
    	int sfd=serial_open("/dev/ttyUSB0");
    	if(sfd==-1)
    	{
        	perror("open");
        	return;
    	}
    	if(serial_config(sfd)!=0)
    	{
        	perror("serial configure");
        	serial_close(sfd);
        	return;
    	}
    	#endif
    	float k = 0;
    	int flag = 1;
    	while(1)
    	{	
    		#if 0
    		static int i = 0;
    		memset(buf, 0, sizeof(buf));
        	int ret=serial_read_image(sfd,buf);
        	if(ret==-1)
        	{
			perror("read");
        		printf("Error number: %d\n", errno);
        		printf("Error string: %s\n", strerror(errno));
        		printf("here\n");
            		return;
        	}
    			//判断距离
	    		//distance = change(buf);
	    	#endif
	    	//printf("distance = %f\n", Hcsr04GetLength);
	    	if(Hcsr04GetLength < 5.0 && flag){
	    		k = Hcsr04GetLength;
	    		flag = 0;
	    		//拍照上传
	    		//cam.c
	    		take_picture(device_path, output_file_path, width, height, pixel_format);
			printf("take photo success: %s\n", output_file_path);
			fp = fopen("photo.jpg", "rb");
			if (fp == NULL) {
				perror("fopen");
				return;
			}
			// 发送数据
			size_t n;
			while ((n = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
				send(connfd, buffer, n, 0);
			}
			
			printf("File sent successfully\n");	
	    	}
	    	if(Hcsr04GetLength - k > 10 || Hcsr04GetLength - k < -10){
	    			flag = 1;
	    	}
	    	// 接收数据
	    	#if 0
		int read_buf = recv(connfd, buf, sizeof(buf) - 1, 0);
		if (read_buf < 0) {
		    if (errno == EAGAIN || errno == EWOULDBLOCK) {
		        // 没有数据可读，继续循环
		        continue;
		    } 
		    else {
		        perror("recv");
		        printf("here\n");
		        //close(connfd);
		        return;
		    }
		} 
		else if (read_buf == 0) {
		    // 连接关闭
		    printf("Connection closed by peer\n");
		    //close(connfd);
		    return;
		}
		buf[read_buf] = '\0'; // 确保字符串结束
		if (strcmp(buf, "open_sg90") == 0) {
		    printf("buf = %s\n", buf);
		    continue;
		} 
		else {
		    printf("error\n");
		}
		#endif
    	}
    	//serial_close(sfd);


}
#endif
